<?php

namespace App\Store\Models\Goods;

use App\Common\Services\Common;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Basic\GoodsStyle;
use Illuminate\Database\Eloquent\Model;

/**
 * 盘点明细
 */
class GoodsCheckInfolist extends Model
{
    protected $table = 'site_goods_check_infolist';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList($request)
    {

        $query = self::with(['storeinfo', 'goods_category', 'goods_style']);

        $goods_category_id = $request['goods_category_id'] ?? 0;
        if ($goods_category_id > 0) {
            $query->where('goods_category_id', $goods_category_id);
        }

        $goods_style_id = $request['goods_style_id'] ?? 0;
        if ($goods_style_id > 0) {
            $query->where('goods_style_id', $goods_style_id);
        }

        $is_check = $request['is_check'] ?? 0;
        if ($is_check > 0) {
            $query->where('is_check', $is_check);
        }

        $ids = $request['ids']??[];
        $ids = is_array($ids) ? $ids : (is_array(json_decode($ids, true)) ? json_decode($ids, true) : []);
        if (count($ids) > 0) {
            $query->whereIn('id', $ids);
        }

        $query->where(['p_id' => $request['p_id'], 'is_del' => 1])->where('count','>',0)->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            $list= Common::paginateDataToArray($list);

        } else {
            $list = $query->get()->toArray();

            $list = ['list'=>$list];
        }

        if(!empty($list['list'])&&isset($list['list'])){

            foreach ($list['list'] as &$item){
                $item['total_weight'] =$item['total_weight']>0?$item['total_weight']:$item['netglod_weight'];
            }
        }

        return  $list;
    }

    public static function getListResult($request)
    {
        $query = self::with(['storeinfo', 'goods_category', 'goods_style'])
            ->where(['p_id' => $request['p_id'], 'is_del' => 1])
            ->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id', 'store_name');
    }

    public function goods_category()
    {
        return $this->hasOne(GoodsCategory::class, 'id', 'goods_category_id')->select('id', 'category_name');
    }

    public function goods_style()
    {
        return $this->hasOne(GoodsStyle::class, 'id', 'goods_style_id')->select('id', 'style_name');
    }
}
